package wypozyczalnia.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import wypozyczalnia.domain.Authorities;
import wypozyczalnia.domain.Movie;
import wypozyczalnia.domain.User;
import wypozyczalnia.forms.RegistrationForm;
import wypozyczalnia.service.AuthoritiesService;
import wypozyczalnia.service.MovieService;
import wypozyczalnia.service.UserService;

@Controller
public class RegistrationController {

	private static final String MSGKEY = "message";

	@Autowired
	UserService userService;

	@Autowired
	AuthoritiesService authService;

	@Resource
	@Qualifier("org.springframework.security.authenticationManager")
	private ProviderManager authenticationManager;

	@Autowired
	MovieService movieService;
	
	@RequestMapping(value = "/register.do")
	public ModelAndView register() {
		List<Movie> movies = movieService.getMovieByGenre(new Integer(1));
		
		return new ModelAndView("register", "registerForm",
				new RegistrationForm());
	}

	@RequestMapping(value = "saveUser.do", method = RequestMethod.POST)
	public String saveUser(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model,
			@ModelAttribute RegistrationForm registrationForm)
			throws IOException {
//
//		User user = new User(registrationForm.getUsername(), registrationForm
//				.getPass(), true, true, true, true, true, registrationForm
//				.getName(), registrationForm.getSecondName(), registrationForm
//				.getEmail(), registrationForm.getPhone(), registrationForm
//				.getDateOfBirth());
//		try {
//			userService.create(user);
//			Authorities role = new Authorities(user, "ROLE_USER");
//			Collection<Authorities> roles = new ArrayList<Authorities>();
//			roles.add(role);
//
//			authService.create(role);
//
//			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
//					user.getUsername(), user.getPassword());
//
//
//			token.setDetails(new WebAuthenticationDetails(request));
//			Authentication authentication = authenticationManager
//					.authenticate(token);
//			SecurityContextHolder.getContext()
//					.setAuthentication(authentication);

//		}
//
//		catch (DataIntegrityViolationException ex) {
//			session
//					.setAttribute(MSGKEY,
//							"Użytkownik o takim loginie istnieje!");
//			model.addAttribute("registerForm", registrationForm);
//			return "register";
//		} catch (Exception e) {
//			SecurityContextHolder.getContext().setAuthentication(null);
//
//		}

		return "start";
	}
}
